var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var fs = require('fs');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// ceshi
var a = require('./routes/congratulate');
var code = require('./routes/we');
var orders = require('./routes/order');
// var loginidentify = 


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// 中间件
var middle = function(req,res,next){
  console.log('request type:' + req.method);
  console.log('中间件:' + Date.now());
  next()
}
app.use(middle);



app.use('/users', usersRouter);
app.get('/we', function (req, res) {
  res.send('GET request to the homepage111')
})
app.use('/login', a);
// app.post('/b', a);
app.use('/code', code);
app.use('/', indexRouter);
// app.all('/order', function(req,res){
//   res.send('http的get和post');
// });
app.get('/order', orders);
app.post('/order', function(req, res, next){
  // res.json({code:500});
  console.log(66666);
  next();
},function(req, res){
  res.send('llllll');
});
app.get('/error', function(req, res){
  // throw new Error('broken');
  fs.readFile("./package.json11", function (err, data) {
    if (err) {
      next(err); // Pass errors to Express.
    }
    else {
      res.send(data);
    }
  });
});
app.get('/manage', function(req, res){
  res.send('get方法');
});
// app.use('/loginidentify',loginidentify);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
app.listen(5000,function(){
    console.log("5000 port on! service up!");
});
module.exports = app;
